package com.huawei.app.demo.fragment;

import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.huawei.app.demo.MainActivity;
import com.huawei.app.demo.R;
import com.huawei.app.demo.util.LogUtils;

/**
 * 白板
 *
 * @since 2021-01-20
 */
public class WhiteboardFragment extends CustomFragment {

    private static final String TAG = WhiteboardFragment.class.getSimpleName();
    private Context context;
    private View rootView;

    @Override
    public String getTitle() {
        return "白板";
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (this.rootView != null) {
            return rootView;
        }
        this.context = getContext();
        this.rootView = inflater.inflate(R.layout.fragment_whiteboard, container, false);

        // init view
        Button openWhiteboard = rootView.findViewById(R.id.openWhiteboard);

        // init event
        openWhiteboard.setOnClickListener(this);
        return rootView;
    }

    @Override
    public void onClick(View v) {
        int viewId = v.getId();
        switch (viewId) {
            case R.id.openWhiteboard: {
                openWhiteboard();
                break;
            }
            default: {
                break;
            }
        }
    }

    public void openWhiteboard() {
        LogUtils.getInstance().i(TAG, "openWhiteboard begin.");
        String pkgName = context.getPackageName();
        String clsName = MainActivity.class.getName();

        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.putExtra("EXTRA_WB_FLAG_SHOW_ANIMATE", true);
        intent.setClassName("com.device.cp","com.device.cp.whiteboard.WhiteboardActivity");
        intent.putExtra("com.device.cp.android.START_ACTIVITY_CHECK", true);
        // 设置退出白板时重新拉起应用的包名和类名
        intent.putExtra("EXTRA_WB_FLAG_OPEN_PACKAGE", pkgName);
        intent.putExtra("EXTRA_WB_FLAG_OPEN_CLASS", clsName);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        try {
            startActivity(intent);
        } catch (ActivityNotFoundException e) {
            LogUtils.getInstance().i(TAG, "openWhiteboard failed, activity not found.");
            openWhiteboardForHub();
        }

        LogUtils.getInstance().i(TAG, "openWhiteboard end.");
    }

    private void openWhiteboardForHub() {
        LogUtils.getInstance().i(TAG, "openWhiteboardForHub begin.");
        String pkgName = context.getPackageName();
        String clsName = MainActivity.class.getName();

        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.putExtra("EXTRA_WB_FLAG_SHOW_ANIMATE", true);
        intent.setClassName("com.huawei.cp", "com.huawei.cp.whiteboard.WhiteboardActivity");
        intent.putExtra("com.huawei.cp.android.START_ACTIVITY_CHECK", true);
        // 设置退出白板时重新拉起应用的包名和类名
        intent.putExtra("EXTRA_WB_FLAG_OPEN_PACKAGE", pkgName);
        intent.putExtra("EXTRA_WB_FLAG_OPEN_CLASS", clsName);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        try {
            startActivity(intent);
        } catch (ActivityNotFoundException e) {
            LogUtils.getInstance().i(TAG, "openWhiteboardForHub failed, activity not found.");
        }

        LogUtils.getInstance().i(TAG, "openWhiteboardForHub end.");
    }
}
